/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * RemoteFileMessageStore.java
 * 
 * Created on Jan 17, 2009 at 12:22:07 PM
 */

package org.robcash.apps.messagepopup.vo.impl;

import java.io.IOException;
import java.net.URL;
import org.robcash.apps.messagepopup.vo.MessageProvider;
import org.robcash.apps.messagepopup.vo.MessageStore;

/**
 *
 * @author Rob
 */
public class RemoteFileMessageStore extends BaseMessageStore
        implements MessageStore {

    /**
     * Message provider
     */
    private MessageProvider messageProvider;

    /**
     * Create a new remote file message store
     * @param location Location in which messages are stored
     */
    public RemoteFileMessageStore(RemoteFileMessageStoreLocation location) {
        super(location);
    }

    /**
     * Get the message provider
     * @return Message provider
     */
    public MessageProvider getMessageProvider() {
        return messageProvider;
    }

    /**
     * Indicates if the message store is valid. The message store is valid if
     * all of the following are true:
     * <ul>
     * <li>The message location can be read</li>
     * <li>More TBD</li>
     * </ul>
     * @return <tt>true</tt> if the message store is valid
     */
    public boolean isValid() {
        URL remoteFile = ((RemoteFileMessageStoreLocation) getLocation()).getRemoteFile();
        try {
            remoteFile.openStream();
            return true;
        }
        catch(IOException e) {
            // Failed to read from remote URL
            return false;
        }
    }

}
